package com.awspaas.user.apps.processforsap.util;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.cc.RDSAPI;
import com.alipay.sofa.common.profile.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class WLUtil {

    /**
     * 根据基础主数据跟新基础视图
     */
    public void createJCST(BO bo, BO mainBO, String bindid) {

        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取类型
        String matlType = bo.getString("MATL_TYPE");
        String matlGroup = bo.getString("MATL_GROUP");
        String plant = bo.getString("PLANT");
        String isMc = mainBO.getString("IS_MC");
        String zassemtype = bo.getString("ZASSEMTYPE");

        //产品层次
        BO bo1 = SDK.getBOAPI().query("BO_EU_SAP_FORCPCC").addQuery("ZASSEMTYPE=", zassemtype).detail();
        if (bo1 != null) {
            bo.set("PROD_HIER", bo1.getString("PROD_HIER"));
        }
        //2025-4-6芝莉老师提出逻辑，只要是V-BP-00，产品层次默认0000900002
        if (matlGroup.equals("V-BP-00")){
            bo.set("PROD_HIER","00009");
        }
        if (matlGroup.equals("V-CP-00")){
            bo.set("PROD_HIER","00010");
        }
        if (matlGroup.equals("V-WL-00")){
            bo.set("PROD_HIER","0000600001");
        }

        if (matlType.equals("FERT") || matlType.equals("HALB")) {
            if (plant.contains("Z")) {
                bo.set("AVAILCHECK", "Z2");
            } else {
                bo.set("AVAILCHECK", "02");
            }
        } else {
            if ("DIEN/Z001/Z002/Z005/ROH".contains(matlType)) {
                bo.set("AVAILCHECK", "02");
            }
            if ("FH0Z/FS0Z/YJ00".contains(plant)) {
                bo.set("AVAILCHECK", "Z2");
            }
            if ("ERSA/Z004/UNBW".contains(matlType)) {
                bo.set("AVAILCHECK", "KP");
            }
            if ("1-WF-00/1-WF-03/2-DM-00/2-DM-01".contains(matlGroup)) {
                bo.set("AVAILCHECK", "KP");
            }
            if ("1".equals(isMc)) {
                bo.set("AVAILCHECK", "Z2");
            }
        }
        if ("YJ00".equals(plant)){
            bo.set("AVAILCHECK", "02");
        }
        //老逻辑
//        if (matlGroup.startsWith("4-") || matlGroup.startsWith("5-") || matlGroup.startsWith("6-")) {
//            bo.set("AVAILCHECK", "KP");
//        }

        if ("V-BP-00".equals(matlGroup) || "V-CP-00".equals(matlGroup)) {
            bo.set("BASE_UOM", "PC");
        }
        if ("V-WL-00".equals(matlGroup) || "1-SM-00".equals(matlGroup) || "V-AO-00".equals(matlGroup) || "V-FO-00".equals(matlGroup) || "V-TK-00".equals(matlGroup) || "V-BG-00".equals(matlGroup)) {
            bo.set("BASE_UOM", "EA");
        }
        String wlmc = bo.getString("WLMC");
        String matlDesc = bo.getString("MATL_DESC");

        String basicMatl = bo.getString("BASIC_MATL");
        bo.set("BASIC_MATL", basicMatl.trim());


        String gg = bo.getString("GG");
        String wlms = (StringUtil.isEmpty(wlmc) ? matlDesc : (wlmc + "|")) + (StringUtil.isEmpty(basicMatl) ? "" : (basicMatl + "|")) + (StringUtil.isEmpty(gg) ? "" : (gg + "|"));
        bo.set("MATL_DESC", wlms);
        String textLine = bo.getString("TEXT_LINE");
        bo.set("TEXT_LINE", wlms);
        System.out.println("--------------开始更新：" + bo.getString("ISXGKC"));
        SDK.getBOAPI().update("BO_EU_SAP_JBST", bo);
    }

    /**
     * 根据基础主数据创建财务视图
     */
    public void createCWST(BO bo, String bindid, String uid, BO mainBo, String pid) {
        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取工厂
        String plant = bo.getString("PLANT");
        //获取PID厂
        List<BO> list = SDK.getBOAPI().query("BO_EU_MM_FORKZJHY").list();
        String id = bo.getId();
        BO cwBo = SDK.getBOAPI().query("BO_EU_SAP_CWST").addQuery("MAINID=", id).detail();
        if (cwBo == null) {
            cwBo = new BO();
            cwBo.set("MAINID", id);
        }

        //获取物料类型
        String matlType = bo.getString("MATL_TYPE");
        //获取物料组
        String matlGroup = bo.getString("MATL_GROUP");
        if ("FH00/FS00/YJ00".contains(plant) && "Z001/Z003/Z004/Z005/ROH/ERSA".contains(matlType)) {
            cwBo.set("ACCOUNT_VIEW", "X");
            cwBo.set("COST_VIEW", "X");
            //评估范围，等于有价厂
            cwBo.set("VAL_AREA", plant);
            cwBo.set("QTY_STRUCT", "X");
            cwBo.set("ORIG_MAT", "X");
            cwBo.set("VARIANCE_KEY", "000001");
        }
        if ("FERT/HALB".contains(matlType) && "FH00/FS00/FT00".contains(plant)) {
            cwBo.set("ACCOUNT_VIEW", "X");
            cwBo.set("COST_VIEW", "X");
            //评估范围，等于有价厂
            cwBo.set("VAL_AREA", plant);
            cwBo.set("QTY_STRUCT", "X");
            cwBo.set("ORIG_MAT", "X");
            cwBo.set("VARIANCE_KEY", "000001");
        }
        if (cwBo != null && "X".equals(cwBo.getString("ACCOUNT_VIEW"))) {
            if (matlType.equals("ROH")) {
                if (matlGroup.startsWith("1-")) {
                    cwBo.set("VAL_CLASS", "3000");
                } else if (matlGroup.startsWith("2-")) {
                    cwBo.set("VAL_CLASS", "3001");
                } else if (matlGroup.startsWith("3-")) {
                    cwBo.set("VAL_CLASS", "3010");
                }
            } else if (matlType.equals("Z001") || "Z005".equals(matlType)) {
                cwBo.set("VAL_CLASS", "3002");
            } else if (matlType.equals("ERSA")) {
                if (matlGroup.startsWith("5-")) {
                    cwBo.set("VAL_CLASS", "3020");
                } else if (matlGroup.startsWith("4-GZ")) {
                    cwBo.set("VAL_CLASS", "3022");
                } else if (matlGroup.startsWith("4-YB") || matlGroup.startsWith("4-YL")) {
                    cwBo.set("VAL_CLASS", "3023");
                }
            } else if (matlType.equals("Z003")) {
                if (matlGroup.startsWith("6")) {
                    if (matlGroup.startsWith("6-GY-00")) {
                        cwBo.set("VAL_CLASS", "3025");
                    } else {
                        cwBo.set("VAL_CLASS", "3021");
                    }
                }
            } else if (matlType.equals("Z004")) {
                if (matlGroup.startsWith("6-KH")) {
                    cwBo.set("VAL_CLASS", "3024");
                }
            } else if (matlType.equals("HALB")) {
                cwBo.set("VAL_CLASS", "7900");
            } else if (matlType.equals("FERT")) {
                cwBo.set("VAL_CLASS", "7920");
            }
            /**
             * 价格控制，价格确定
             */
            if ("ROH/ERSA/Z003/Z004/Z001/Z005".contains(matlType)) {
                if ("FH00/FS00/YJ00".contains(plant)) {
                    cwBo.set("PRICE_CTRL", "V");
                    cwBo.set("ML_SETTLE", "2");
                    if ("Z001/Z005/ROH".contains(matlType)) {
                        cwBo.set("PRICE_UNIT", "10000");
                        cwBo.set("LOT_SIZE", "10000");
                    } else if ("Z003/Z004/ERSA".contains(matlType)) {
                        cwBo.set("PRICE_UNIT", "1");
                        cwBo.set("LOT_SIZE", "1");
                    }
                }
            } else if (matlType.equals("FERT") || matlType.equals("HALB")) {
                //成本核算批量
                if (plant.equals("FT00")) {
                    cwBo.set("PRICE_CTRL", "V");
                    cwBo.set("ML_SETTLE", "2");
                } else {
                    cwBo.set("PRICE_CTRL", "S");
                    cwBo.set("ML_SETTLE", "3");
                }
                if ("V-AO-00/V-TK-00/V-FO-00/1-SM-00/V-WL-00".contains(matlGroup)) {
                    cwBo.set("PRICE_UNIT", "10000");
                    cwBo.set("LOT_SIZE", "10000");
                } else if ("V-CP-00/V-BP-00".contains(matlGroup)) {
                    cwBo.set("PRICE_UNIT", "1");
                    cwBo.set("LOT_SIZE", "1");
                }
            }
            //利润中心
            if ("Z001/Z005/ROH/ERSA/Z004".contains(matlType)) {
                if (plant.equals("FH00")) {
                    cwBo.set("PROFIT_CTR", "F1S");
                }
                if (plant.equals("FS00")) {
                    cwBo.set("PROFIT_CTR", "F2S");
                }
                if (plant.equals("YJ00")) {
                    cwBo.set("PROFIT_CTR", "YJ");
                }
            } else if ("Z003".equals(matlType)) {
                if (matlGroup.startsWith("6-GY-")) {
                    cwBo.set("PROFIT_CTR", "HQ");

                } else {
                    if (plant.equals("FH00")) {
                        cwBo.set("PROFIT_CTR", "F1S");
                    }
                    if (plant.equals("FS00")) {
                        cwBo.set("PROFIT_CTR", "F2S");
                    }
                }
            }
            if ("FH00/FS00/FT00".contains(plant)) {
                if ("FERT/HALB".contains(matlType)) {
                    String outway = mainBo.getString("OUTWAY");
                    if ("FT00".equals(plant)) {
                        cwBo.set("PROFIT_CTR", "FT");
                    } else {
                        cwBo.set("PROFIT_CTR", pid);
                    }
                    if (StringUtil.isNotEmpty(outway)) {
                        if (plant.equals("FH00")) {
                            cwBo.set("PROFIT_CTR", "F1S");
                        }
                        if (plant.equals("FS00")) {
                            cwBo.set("PROFIT_CTR", pid);
                        }
                        if (plant.equals("FT00")) {
                            cwBo.set("PROFIT_CTR", "FT");
                        }
                    }
                }
            }
        }
        if (matlType.equals("Z006")){
            cwBo.set("ACCOUNT_VIEW","X");
            cwBo.set("COST_VIEW","ZW02");
            cwBo.set("VAL_AREA",plant);
            cwBo.set("VAL_CLASS","3021");
            cwBo.set("PRICE_CTRL","V");
            cwBo.set("ML_SETTLE","2");
            cwBo.set("PRICE_UNIT","1");
            cwBo.set("QTY_STRUCT","X");
            cwBo.set("ORIG_MAT","X");
            cwBo.set("VARIANCE_KEY","0000001");
            cwBo.set("PROFIT_CTR","HQ");
            cwBo.set("LOT_SIZE","1");
        }

        if (StringUtil.isNotEmpty(cwBo.getId())) {
            SDK.getBOAPI().update("BO_EU_SAP_CWST", cwBo);
        } else {
            SDK.getBOAPI().create("BO_EU_SAP_CWST", cwBo, bindid, uid);
        }
    }

    /**
     * 根据基础主数据创建采购试图
     */
    public void createCGST(BO bo, String bindid, BO mainBo, String uid) {
        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取工厂
        String plant = bo.getString("PLANT");
        String outWay = mainBo.getString("OUTWAY");
        String id = bo.getId();
        BO cgBo = SDK.getBOAPI().query("BO_EU_SAP_CGST").addQuery("MAINID=", id).detail();
        if (cgBo == null) {
            cgBo = new BO();
            cgBo.set("MAINID", id);
        }

        //获取物料类型
        String matlType = bo.getString("MATL_TYPE");
        //获取物料组
        String matlGroup = bo.getString("MATL_GROUP");

        if ("FERT/HALB".contains(matlType)) {
            if (StringUtil.isNotEmpty(outWay)) {
                cgBo.set("PURCHASE_VIEW", "X");
                cgBo.set("PUR_GROUP", "230");
            }
        }
        if ("ROH/ERSA/Z003/Z004/Z001/Z005/Z006".contains(matlType)) {
            if ("FH00/FH0Z/FS00/FS0Z/YJ00".contains(plant)) {
                cgBo.set("PURCHASE_VIEW", "X");
                if ("ROH".equals(matlType)) {
                    //原材料逻辑不通
                    if (matlGroup.startsWith("1-")) {
                        if (matlGroup.startsWith("1-SB") || matlGroup.startsWith("1-LF")) {
                            cgBo.set("PUR_GROUP", "100");
                        } else {
                            cgBo.set("PUR_GROUP", "110");
                        }
                    }
                    if (matlGroup.startsWith("2-")) {
                        cgBo.set("PUR_GROUP", "130");
                    }
                    if (matlGroup.startsWith("3-")) {
                        cgBo.set("PUR_GROUP", "120");
                    }
                    if (matlGroup.startsWith("4-")) {
                        cgBo.set("PUR_GROUP", "210");
                    }
                    if (matlGroup.startsWith("5-CW")) {
                        cgBo.set("PUR_GROUP", "220");
                    }
                    if (matlGroup.startsWith("5-BJ") || matlGroup.startsWith("6-")) {
                        cgBo.set("PUR_GROUP", "200");
                    }
                } else if ("Z001/Z005".contains(matlType)) {
                    cgBo.set("PUR_GROUP", "190");
                } else {
                    if ("4-GZ-00/4-YB-00/4-YL-00/6-KH-00".contains(matlGroup)) {
                        cgBo.set("PUR_GROUP", "220");
                    } else if ("5-BJ-00/6-CM-00/6-DE-00/6-HC-00/6-LB-00".contains(matlGroup)) {
                        cgBo.set("PUR_GROUP", "200");
                    } else if ("5-CW-00".contains(matlGroup)) {
                        cgBo.set("PUR_GROUP", "210");
                    } else if ("6-GY-00".contains(matlGroup)) {
                        cgBo.set("PUR_GROUP", "300");
                    }
                }
                if ("ERSA/Z003/Z004/ROH".contains(matlType)) {
                    cgBo.set("AUTO_P_ORD", "X");
                    if (!matlGroup.startsWith("6-GY")) {
                        cgBo.set("AUTO_P_ORD", "X");
                    }
                    if ("Z003".equals(matlType) && "6-GY-00".equals(matlGroup)) {
                        cgBo.set("PURCHASE_VIEW", "X");
                        cgBo.set("PUR_GROUP", "300");
                        cgBo.set("AUTO_P_ORD", "X");
                        cgBo.set("SOURCELIST", "X");
                    }
                }
                if ("Z006".equals(matlType)) {
                    cgBo.set("PURCHASE_VIEW", "X");
                    cgBo.set("PUR_GROUP", "300");
                    cgBo.set("AUTO_P_ORD", "X");
                    cgBo.set("SOURCELIST", "X");
                }
            }
        }
        if (StringUtil.isNotEmpty(cgBo.getId())) {
            SDK.getBOAPI().update("BO_EU_SAP_CGST", cgBo);
        } else {
            SDK.getBOAPI().create("BO_EU_SAP_CGST", cgBo, bindid, uid);
        }
    }

    /**
     * 根据基础主数据创建存储视图
     */
    public void createCCST(BO bo, String bindid, String uid) {
        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取工厂
        String plant = bo.getString("PLANT");
        String id = bo.getId();
        BO ccBo = SDK.getBOAPI().query("BO_EU_SAP_CCST").addQuery("MAINID=", id).detail();
        if (ccBo == null) {
            ccBo = new BO();
            ccBo.set("MAINID", id);

        }
        //获取物料类型
        String matlType = bo.getString("MATL_TYPE");
        //获取物料组
        String matlGroup = bo.getString("MATL_GROUP");
        //获取客户三码
        String pageformat = bo.getString("PAGEFORMAT");

        if ("FERT/HALB".contains(matlType)) {
            if ("FH00/FS00/FH0Z/FS0Z/FT00".contains(plant)) {
                ccBo.set("STORAGE_VIEW", "X");
                ccBo.set("BATCH_MGMT_GL", "X");
                ccBo.set("BATCH_MGMT_BS", "X");
                if ("V-AO-00/V-CP-00/V-BP-00/V-WL-00/V-FO-00/V-TK-00".contains(matlGroup)) {
                    ccBo.set("MINREMLIFE", 2);
                    ccBo.set("SHELF_LIFE", 365);
                    ccBo.set("PERIOD_IND_EXPIRATION_DATE", "D");
                }
                if ("FT00".equals(plant) && "FERT".equals(matlType)) {
                    ccBo.set("MINREMLIFE", 2);
                    ccBo.set("SHELF_LIFE", 365);
                    ccBo.set("PERIOD_IND_EXPIRATION_DATE", "D");
                }
            }
        } else if ("ERSA/Z004/ROH/Z003/Z001/Z002/Z005/UNBW".contains(matlType)) {
            ccBo.set("STORAGE_VIEW", "X");

            if ("UNBW".equals(matlType)) {
                ccBo.set("STGE_LOC", "SK92");
            }
            if ("6-GY-00".equals(matlGroup)) {
                ccBo.set("STGE_LOC", "ZW03");
            }
            if ("ROH".equals(matlType) && matlGroup.startsWith("1-WF")) {
                if ("FH0Z".equals(plant)) {
                    ccBo.set("STGE_LOC", "0001");
                }
                if ("FS0Z".equals(plant)) {
                    ccBo.set("STGE_LOC", "0051");
                }
            }
            if ("FH00".equals(plant)) {
                if (matlGroup.equals("6-CW-00")) {
                    ccBo.set("STGE_LOC", "SK05");
                }
                if (matlGroup.startsWith("6-KH")) {
                    ccBo.set("STGE_LOC", "SK08");
                }
                if (matlGroup.startsWith("6-") && (!matlGroup.startsWith("6-KH") && !matlGroup.startsWith("6-CW-00"))) {
                    ccBo.set("STGE_LOC", "SK03");
                }
                if (matlType.equals("ERSA/Z004")) {
                    if ("4-YL-00/5-BJ-00/5-CW-00/4-GZ-00/4-YB-00".contains(matlGroup)) {
                        ccBo.set("STGE_LOC", "SK58");
                    }
                }
                if (matlGroup.equals("6-GY-00")) {
                    ccBo.set("STGE_LOC", "ZW03");
                }
            }
            if ("FS00".equals(plant)) {
                if (matlGroup.equals("6-CW-00")) {
                    ccBo.set("STGE_LOC", "SK55");
                }
                if (matlGroup.startsWith("6-KH")) {
                    ccBo.set("STGE_LOC", "SK58");
                }
                if (matlGroup.startsWith("6-") && (!matlGroup.startsWith("6-KH") && !matlGroup.startsWith("6-CW-00"))) {
                    ccBo.set("STGE_LOC", "SK53");
                }
            }


            if ("ERSA/Z004".contains(matlType)) {
                if ("FH00".equals(plant)) {
                    ccBo.set("STGE_LOC", "SK08");
                }
                if ("FS00".equals(plant)) {
                    ccBo.set("STGE_LOC", "SK58");
                }
            }

            if ("ROH".equals(matlType)){
                BO bo1  = SDK.getBOAPI().query("BO_EU_SAP_CCDD").addQuery("PLANT=",plant).addQuery("MATL_GROUP=",matlGroup).detail();
                if (bo1!=null){
                    ccBo.set("STGE_LOC", bo1.getString("CCDD"));
                }
            }

            if ("ERSA/Z004/ROH/Z003/Z001/Z002/Z005".contains(matlType)) {
                ccBo.set("BATCH_MGMT_GL", "X");
                ccBo.set("BATCH_MGMT_BS", "X");
            }
            if ("6-CM-00/6-DE-00/6-GY-00/6-HC-00/6-LB-00".contains(matlGroup)) {
                ccBo.set("BATCH_MGMT_GL", "X");
                ccBo.set("BATCH_MGMT_BS", "X");
            }


            if ("FH00/FS00/FH0Z/FS0Z/YJ00".contains(plant)) {
                if ("ROH".equals(matlType)) {
                    ccBo.set("MINREMLIFE", "3");
                }
                if ("Z001/Z002/Z005".contains(matlType) && "FH00/FS00/FH0Z/FS0Z".contains(plant)) {
                    ccBo.set("MINREMLIFE", "2");
                }
                if ("ERSA/Z003/UNBW".equals(matlType)) {
                    ccBo.set("MINREMLIFE", "1");
                }
            }

            //总货架寿命
            if ("Z003/ERSA/Z004".contains(matlType)) {
                ccBo.set("SHELF_LIFE", 9999);
            }
            if ("FH00/FS00/FH0Z/FS0Z".contains(plant)) {
                if ("Z001/Z002/Z005/ROH".contains(matlType)) {
                    String rdsId = SDK.getRuleAPI().executeAtScript("@getSapParamas(SAP_MID_ID)");
                    RDSAPI api = SDK.getCCAPI().getRDSAPI(rdsId);
                    String sql = "select * from CRM_CUSTMATVALIDITYDATE where MATCLASS = ? and CUSTCODE = ? and MATTYPE IS NULL";
                    RowMap rowMap = api.getMap(sql, matlGroup, pageformat);
                    SDK.getProcessAPI().setVariable(bindid, "VALIDITYDATE", "true");
                    if (rowMap == null) {
                        RowMap rowMap1 = api.getMap(sql, matlGroup, "NORMAL");
                        if (rowMap1 == null) {

                        } else {
                            String validitydate = rowMap1.getString("VALIDITYDATE");
                            if ("0".equals(validitydate)) {
                                SDK.getProcessAPI().setVariable(bindid, "VALIDITYDATE", "false");
                            } else {
                                ccBo.set("SHELF_LIFE", validitydate);
                            }
                        }
                    } else {
                        String validitydate = rowMap.getString("VALIDITYDATE");
                        if ("0".equals(validitydate)) {
                            SDK.getProcessAPI().setVariable(bindid, "VALIDITYDATE", "false");
                        } else {
                            ccBo.set("SHELF_LIFE", validitydate);
                        }
                    }
                }
            }
            ccBo.set("PERIOD_IND_EXPIRATION_DATE", "D");
        }
        if (matlType.equals("Z006")){
            ccBo.set("STORAGE_VIEW","X");
            ccBo.set("STGE_LOC","ZW02");
        }
        if (StringUtil.isNotEmpty(ccBo.getId())) {
            SDK.getBOAPI().update("BO_EU_SAP_CCST", ccBo);
        } else {
            SDK.getBOAPI().create("BO_EU_SAP_CCST", ccBo, bindid, uid);
        }
    }

    /**
     * 根据基础主数据创建MRP视图
     */
    public void createMRPST(BO bo, String bindid, String uid, BO mainBO, String pid) {
        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取工厂
        String plant = bo.getString("PLANT");
        String khsm = bo.getString("PAGEFORMAT");
        String wwfs = mainBO.getString("OUTWAY");
        String id = bo.getId();
        List<BO> list = SDK.getBOAPI().query("BO_EU_MM_FORKZJHY").list();
        BO mrpBo = SDK.getBOAPI().query("BO_EU_SAP_MRP").addQuery("MAINID=", id).detail();
        if (mrpBo == null) {
            mrpBo = new BO();
            mrpBo.set("MAINID", id);
        }

        //获取物料类型
        String matlType = bo.getString("MATL_TYPE");
        //获取物料组
        String matlGroup = bo.getString("MATL_GROUP");
        String material = bo.getString("MATERIAL");

        if ("YJ00".equals(plant)){

        }else{
            if ("FERT/HALB".contains(matlType)) {
                if ("FH00/FS00".contains(plant)) {
                    mrpBo.set("MRP_VIEW", "X");
                    if (matlGroup.startsWith("1-SM-00")) {
                        mrpBo.set("MRP_GROUP", "0070");
                    } else {
                        mrpBo.set("MRP_GROUP", "0050");
                        mrpBo.set("PL_TI_FNCE", "0");
                    }
                    mrpBo.set("MRP_TYPE", "M0");
                    mrpBo.set("MRP_CTRLER", StringUtil.isEmpty(pid) ? "" : "F" + pid);
                    mrpBo.set("LOTSIZEKEY", "EX");
                    mrpBo.set("PUR_STATUS", "B");
                    mrpBo.set("BERID", plant + "-ND");
                    mrpBo.set("DISMM", "ND");
                    if (StringUtil.isNotEmpty(wwfs)) {
                        if ("FH00".equals(plant)) {
                            mrpBo.set("PROC_TYPE", "F");
                        } else {
                            mrpBo.set("PROC_TYPE", "E");
                        }
                    } else {
                        mrpBo.set("PROC_TYPE", "E");
                    }
                    String procTypep = mrpBo.getString("PROC_TYPE");
                    if (StringUtil.equals("F", procTypep)) {
                        if (wwfs.equals("STO")) {
                            mrpBo.set("SPPROCTYPE", "40");
                        } else if (wwfs.equals("PO")) {
                            mrpBo.set("SPPROCTYPE", "30");
                        }

                        if (khsm.equals("MCG") && "STO".equals(wwfs)) {
                            mrpBo.set("SLOC_EXPRC", "N101");
                        } else {
                            mrpBo.set("SLOC_EXPRC", "N201");
                        }
                    }
                    if (matlGroup.equals("1-SM-00")) {
                        mrpBo.set("INHSEPRODT", "2");
                    }
                    if (matlGroup.equals("V-AO-00")) {
                        mrpBo.set("INHSEPRODT", "8");
                    }
                    if (matlGroup.equals("V-FO-00")) {
                        mrpBo.set("INHSEPRODT", "4");
                    }
                    if (matlGroup.equals("V-TK-00")) {
                        if (material.startsWith("V-TK")) {
                            mrpBo.set("INHSEPRODT", "3");
                        } else {
                            mrpBo.set("INHSEPRODT", "8");
                        }
                    }
                    if (matlGroup.equals("V-BP-00")) {
                        mrpBo.set("INHSEPRODT", "6");
                    }
                    if (matlGroup.equals("V-CP-00")) {
                        mrpBo.set("INHSEPRODT", "4");
                    }
                    if (matlGroup.equals("V-WL-00")) {
                        mrpBo.set("INHSEPRODT", "7");
                    }
                    if (matlGroup.equals("V-BG-00")) {
                        mrpBo.set("INHSEPRODT", "4");
                    }
                    mrpBo.set("SM_KEY", "000");
                    mrpBo.set("CONSUMMODE", "5");
                    if (matlGroup.startsWith("1-SM")) {
                        mrpBo.set("DEP_REQ_ID", "2");
                    } else {
                        mrpBo.set("DEP_REQ_ID", "1");
                    }
                }
            }


            String isxgkc = bo.getString("ISXGKC");
            if (!"1".equals(isxgkc)) {
                if (("Z001/Z002/Z005".contains(matlType) && "FS00/FH00/FS0Z/FH0Z/YJ00".contains(plant))
                        || ("FH00/FS00/YJ00".contains(plant) && matlType.equals("ROH"))
                        || ("FH0Z/FS0Z".contains(plant) && matlType.equals("ROH") && (matlGroup.startsWith("1-WF") || matlGroup.startsWith("2-DM")))) {
                    mrpBo.set("MRP_VIEW", "X");

                    if ("Z001/Z005/Z002/ROH".contains(matlType) && !"YJ00".equals(plant)) {
                        mrpBo.set("MRP_GROUP", "0070");
                    }
                    if ("Z001/Z005/Z002/ROH".contains(matlType)) {
                        if ("FS00/FH00".contains(plant)) {
                            if (matlGroup.startsWith("1-") && !matlGroup.startsWith("1-WF")) {
                                mrpBo.set("MRP_TYPE", "PD");

                            }
                        } else if ("FS0Z/FH0Z/YJ00".contains(plant)) {
                            if (matlGroup.startsWith("1-") && !matlGroup.startsWith("1-WF")) {
                                mrpBo.set("MRP_TYPE", "ND");

                            }
                        }
                    }
                    if (matlGroup.startsWith("1-WF") || matlGroup.startsWith("2-") || matlGroup.startsWith("3-")) {
                        mrpBo.set("MRP_TYPE", "ND");
                    }
                    if ("ROH".equals(matlType)) {
                        String result = "";
                        for (BO bo1 : list) {
                            String matlGroup1 = bo1.getString("MATL_GROUP");
                            String plant1 = bo1.getString("PLANT");
                            String pd = bo1.getString("PD");
                            String startnum = bo1.getString("STARTNUM");
                            String[] matlGroups = matlGroup1.split("/");
                            if (plant.equals(plant1)) {
                                for (String mg : matlGroups) {
                                    if ("1".equals(pd) && matlGroup.contains(mg)) {
                                        result = bo1.getString("KZYDM");
                                    } else if ("0".equals(pd) && matlGroup.startsWith(startnum)) {
                                        result = bo1.getString("KZYDM");
                                    }
                                }
                            }

                        }
                        mrpBo.set("MRP_CTRLER", result);
                    }
                    if ("Z001/Z002/Z005".contains(matlType)) {
                        if ("FH00".equals(plant)) {
                            mrpBo.set("MRP_CTRLER", "A80");
                            mrpBo.set("LOTSIZEKEY", "EX");
                        }
                        if ("FS00".equals(plant)) {
                            mrpBo.set("MRP_CTRLER", "B80");
                            mrpBo.set("LOTSIZEKEY", "EX");
                        }
                    }
                    if ("ROH".equals(matlType)) {
                        if ("FH00/FS00".contains(plant)) {
                            mrpBo.set("LOTSIZEKEY", "WB");
                        }
                    }
                    if ("Z001/Z002/Z005/ROH".contains(matlType)) {
                        if ("FH00/FS00".contains(plant)) {
                            mrpBo.set("BERID", plant + "-ND");
                            mrpBo.set("DISMM", "ND");
                            mrpBo.set("PLAN_STRGP", "70");
                            mrpBo.set("CONSUMMODE", "5");
                        }
                    }
                    if ("Z001/Z002/Z005/ROH".contains(matlType)) {
                        if ("FH00/FS00/YJ00/FH0Z/FS0Z".contains(plant)) {
                            mrpBo.set("PROC_TYPE", "F");
                            mrpBo.set("DEP_REQ_ID", "2");
                        }
                    }
                    if ("ROH".equals(matlType)) {
                        if ("FH00/FS00".contains(plant)) {
                            mrpBo.set("GR_PR_TIME", "3");
                        }
                    }
                    mrpBo.set("SM_KEY", "000");

                    if (plant.endsWith("Z")){
                        if ("Z001/Z002/Z005/ROH".contains(matlType)){
                            mrpBo.set("MRP_GROUP","");
                            mrpBo.set("SM_KEY", "");
                        }
                    }
                    if ("YJ00".equals(plant)){
                        mrpBo.set("SM_KEY", "");

                    }

                    if (material.startsWith("S")){
                        if ("FH00".equals(plant)){
                            mrpBo.set("MRP_CTRLER","FA1");
                        }
                        if("FS00".equals(plant)){
                            mrpBo.set("MRP_CTRLER","FA6");
                        }
                    }
                }
            }
        }
        if (StringUtil.isNotEmpty(mrpBo.getId())) {
            SDK.getBOAPI().update("BO_EU_SAP_MRP", mrpBo);
        } else {
            SDK.getBOAPI().create("BO_EU_SAP_MRP", mrpBo, bindid, uid);
        }
    }

    /**
     * 根据基础主数据创建销售视图
     */
    public void createXSST(BO bo, String bindid, String uid, BO mainBo) {
        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取工厂
        String plant = bo.getString("PLANT");
        String id = bo.getId();

        String rdsId = SDK.getRuleAPI().executeAtScript("@getSapParamas(SAP_MID_ID)");
        RDSAPI api = SDK.getCCAPI().getRDSAPI(rdsId);


        String pageformat = bo.getString("PAGEFORMAT");

        BO flBo = SDK.getBOAPI().query("BO_EU_SAP_XSST").addQuery("MAINID=", id).detail();
        if (flBo == null) {
            flBo = new BO();
            flBo.set("MAINID", id);
        }
//获取币种
        String currency = mainBo.getString("CURRENCY");
        //获取物料类型
        String matlType = bo.getString("MATL_TYPE");
        //获取物料组
        String matlGroup = bo.getString("MATL_GROUP");
        if ("FERT/HALB".contains(matlType)) {
            if ("V-AO-00/V-CP-00/V-BP-00/V-WL-00/V-FO-00/V-TK-00".contains(matlGroup)) {
                if ("FH00/FH0Z/FS00/FS0Z/FT00".contains(plant)) {
                    String newPlant = plant.substring(0, 3) + "0";
                    flBo.set("SALES_VIEW", "X");
                    flBo.set("SALES_ORG", plant.substring(0, 3) + "0");
                    flBo.set("DISTR_CHAN", "00");
                    flBo.set("DEPCOUNTRY", "CN");
                    flBo.set("TAXCLASS_4", currency.equals("CNY") ? "1" : "0");
                    flBo.set("TAX_TYPE_4", "MWST");
                    //根据客户三码,查SAP中间库销售台账，获取结算方式
                    String sql1 = "select * from SAP_CUSTOMER_SALES where KUNNR = '" + pageformat + "'";
                    RowMap rowMap1 = api.getMap(sql1);
                    if (rowMap1 != null) {
                        String vsbed = rowMap1.getString("VSBED");
                        if ("01".equals(vsbed)) {
                            flBo.set("ITEM_CAT", "Z100");
                        }
                        if ("02".equals(vsbed)) {
                            flBo.set("ITEM_CAT", "Z000");
                        }
                        if ("03".equals(vsbed)) {
                            flBo.set("ITEM_CAT", "Z200");
                        }
                        if ("03".equals(vsbed) && matlType.equals("FERT") && (matlGroup.equals("V-BP-00") || matlGroup.equals("V-CP-00") || matlGroup.equals("V-AO-00") || matlGroup.equals("V-WL-00"))) {
                            flBo.set("ITEM_CAT", "Z100");
                        }
                    }
                    flBo.set("ACCT_ASSGT", currency.equals("CNY") ? "01" : "03");
                    flBo.set("LOADINGGRP", "0001");
                }
            }

        }
        if ("Z001/Z002/Z004/Z005/ROH/DIEN".contains(matlType)) {
            if ("FH00/FH0Z/FS00/FS0Z/YJ00/FT00".contains(plant)) {
                flBo.set("SALES_VIEW", "X");
                if (bo.getString("ISXGKC").equals("1")) {
                    flBo.set("SALES_ORG", "FT00");
                } else {
                    flBo.set("SALES_ORG", plant.substring(0, 3) + "0");
                }
                flBo.set("DISTR_CHAN", "00");
                flBo.set("DEPCOUNTRY", "CN");
                flBo.set("TAXCLASS_4", currency.equals("CNY") ? "1" : "0");
                flBo.set("TAX_TYPE_4", "MWST");
                //根据客户三码,查SAP中间库销售台账，获取结算方式
                if ("Z001/Z002/Z005".contains(matlType)) {
                    flBo.set("ITEM_CAT", "Z000");
                } else if ("ROH".equals(matlType)) {
                    flBo.set("ITEM_CAT", "NORM");
                } else if ("DIEN".equals(matlType)) {
                    flBo.set("ITEM_CAT", "ZN20");
                } else if ("Z004".equals(matlType)) {
                    flBo.set("ITEM_CAT", "ZN10");
                }
                if ("Z001".equals(matlType)) {
                    flBo.set("ACCT_ASSGT", "03");
                }
                if ("Z002".equals(matlType)) {
                    flBo.set("ACCT_ASSGT", "01");
                }
                if ("Z005".equals(matlType)) {
                    flBo.set("ACCT_ASSGT", "05");
                }
                if ("ROH".equals(matlType)) {
                    flBo.set("ACCT_ASSGT", "05");
                }
                if ("Z004".equals(matlType)) {
                    flBo.set("ACCT_ASSGT", "04");
                }
                //TODO DIEN自行填写，需要卡必填
                if ("Z001/Z002/Z004/Z005/DIEN/ROH".contains(matlType)) {
                    flBo.set("LOADINGGRP", "0001");
                }
            }
        }
        if (StringUtil.isNotEmpty(flBo.getId())) {
            SDK.getBOAPI().update("BO_EU_SAP_XSST", flBo);
        } else {
            SDK.getBOAPI().create("BO_EU_SAP_XSST", flBo, bindid, uid);
        }
    }

    /**
     * 根据基础主数据创建分类视图
     */
    public void createFLST(BO bo, String bindid, String uid, BO mainBo) {
        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取工厂
        String plant = bo.getString("PLANT");
        //获取物料类型
        String matlType = bo.getString("MATL_TYPE");
        //获取物料组
        String matlGroup = bo.getString("MATL_GROUP");

        String id = bo.getId();
        BO flBo = SDK.getBOAPI().query("BO_EU_SAP_FLST").addQuery("MAINID=", id).detail();
        if (flBo == null) {
            flBo = new BO();
            flBo.set("MAINID", id);
        }


        if ("FERT/HALB".contains(matlType)) {
            flBo.set("CLASS_VIEW", "X");
            if (!plant.endsWith("Z")) {
                flBo.set("CLASSTYPE", "001");
                flBo.set("CLASSNUM", "Z_PROD_MAT");
            }else if (!matlGroup.endsWith("1-SM-00")){
                flBo.set("CLASSTYPE", "023");
                flBo.set("CLASSNUM", "Z_PROD_BATCH");
            }
        }
        if ("Z001/Z002/Z003/Z004/Z005/ROH/ERSA/Z006/DIEN/UNBW".contains(matlType)) {
            flBo.set("CLASS_VIEW", "X");
            flBo.set("CLASSTYPE", "001");
            if ("Z001/Z005/Z002/ROH".contains(matlType)) {
                if ("FH00/FS00".contains(plant)) {
                    if (matlGroup.startsWith("1-WF")) {
                        flBo.set("CLASSNUM", "Z_WAFER_MAT");
                    }else if ("1-IC-00".equals(matlGroup) || "1-IC-E0".equals(matlGroup)){
                        flBo.set("CLASSNUM", "Z_PROD_MAT");
                    }else {
                        flBo.set("CLASSNUM", "Z_ROH_MAT");
                    }
                }
                if ("FH0Z/FS0Z".contains(plant)) {
                    if (matlGroup.startsWith("1-WF")) {
                        flBo.set("CLASSTYPE", "023");
                        flBo.set("CLASSNUM", "Z_WAFER_BATCH");
                    }else if ("1-IC-00".equals(matlGroup) || "1-IC-E0".equals(matlGroup)){
                        flBo.set("CLASSTYPE", "023");
                        flBo.set("CLASSNUM", "Z_PROD_BATCH");
                    } else {
                        flBo.set("CLASSTYPE", "023");
                        flBo.set("CLASSNUM", "Z_ROH_BATCH");
                    }
                }
            } else if ("Z002".equals(matlType)) {
                if ("FH00/FS00".contains(plant)) {
                    if (matlGroup.startsWith("1-WF")) {
                        flBo.set("CLASSNUM", "Z_WAFER_MAT");
                    } else if (matlGroup.startsWith("1-IC")) {
                        flBo.set("CLASSNUM", "Z_PROD_MAT");
                    } else {
                        flBo.set("CLASSNUM", "Z_ROH_MAT");
                    }
                }
                if ("FH0Z/FS0Z".contains(plant)) {
                    if (matlGroup.startsWith("1-WF")) {
                        flBo.set("CLASSTYPE", "023");
                        flBo.set("CLASSNUM", "Z_WAFER_BATCH");
                    } else if (matlGroup.startsWith("1-IC")) {
                        flBo.set("CLASSTYPE", "023");
                        flBo.set("CLASSNUM", "Z_PROD_BATCH");
                    } else {
                        flBo.set("CLASSTYPE", "023");
                        flBo.set("CLASSNUM", "Z_ROH_BATCH");
                    }
                }
            } else if ("Z003".equals(matlType)) {
                if ("FH00/FS00".contains(plant)) {
                    flBo.set("CLASSTYPE", "001");
                    flBo.set("CLASSNUM", "Z_ROH_MAT");
                }
            } else if ("ERSA/Z004".contains(matlType)) {
                if ("FH00/FS00".contains(plant)) {
                    flBo.set("CLASSTYPE", "001");
                    flBo.set("CLASSNUM", "Z_ROH_BATCH");
                }
            }
            if (plant.endsWith("Z")){
                if (matlGroup.startsWith("1-IC-00")) {
                    flBo.set("CLASSTYPE", "023");
                    flBo.set("CLASSNUM", "Z_PROD_BATCH");
                } else if (matlGroup.startsWith("1-IC-01")) {
                    flBo.set("CLASSTYPE", "023");
                    flBo.set("CLASSNUM", "Z_ROH_BATCH");
                }
            }else{
                if (matlGroup.startsWith("1-IC-00")) {
                    flBo.set("CLASSTYPE", "001");
                    flBo.set("CLASSNUM", "Z_PROD_MAT");
                } else if (matlGroup.startsWith("1-IC-01")) {
                    flBo.set("CLASSTYPE", "001");
                    flBo.set("CLASSNUM", "Z_ROH_MAT");
                }
            }
        }




        if (StringUtil.isNotEmpty(flBo.getId())) {
            SDK.getBOAPI().update("BO_EU_SAP_FLST", flBo);
        } else {
            SDK.getBOAPI().create("BO_EU_SAP_FLST", flBo, bindid, uid);
        }
    }

    /**
     * 根据基础主数据创建工作计划视图
     */
    public void createGZJHST(BO bo, String bindid, String uid, BO mainBo, String pid) {
        /**
         * 逻辑一：如果是有价厂(FH00 电子有价、FS00 半导体有价、FT00 甬矽香港、YJ00  鲸致)，开启财务视图，非有价厂不开启
         */
        //获取工厂
        String plant = bo.getString("PLANT");
        //需要去SAP试图里面做查询

        //获取币种
        String currency = mainBo.getString("CURRENCY");
        String id = bo.getId();
        BO flBo = SDK.getBOAPI().query("BO_EU_SAP_GZJH").addQuery("MAINID=", id).detail();
        if (flBo == null) {
            flBo = new BO();
            flBo.set("MAINID", id);
        }

        //获取物料类型
        String matlType = bo.getString("MATL_TYPE");
        //获取物料组
        String matlGroup = bo.getString("MATL_GROUP");

        if ("FERT/HALB".contains(matlType)) {
            if (plant.endsWith("Z")||plant.equals("FT00")){

            }else{
                flBo.set("WORK_SCHED_VIEW", "X");
                flBo.set("PRODUCTION_SCHEDULER", "F" + pid);
            }
        }
        if ("Z001/Z002/Z003/Z004/Z005/ROH/ERSA".contains(matlType)) {

        }

        if (StringUtil.isNotEmpty(flBo.getId())) {
            SDK.getBOAPI().update("BO_EU_SAP_GZJH", flBo);
        } else {
            SDK.getBOAPI().create("BO_EU_SAP_GZJH", flBo, bindid, uid);
        }
    }

    public void createLod(String logInfo){
        System.out.println(logInfo);
    }
}
